package com.explorati.design_pattern.structural.proxy.staticproxy;

import com.explorati.design_pattern.structural.proxy.IOrderService;
import com.explorati.design_pattern.structural.proxy.Order;
import com.explorati.design_pattern.structural.proxy.OrderServiceImpl;
import com.explorati.design_pattern.structural.proxy.db.DataSourceContextHolder;

public class OrderServiceStaticProxy {

	private IOrderService iOrderService;

	public int saveOrder(Order order) {
		beforeMethod(order);
		iOrderService = new OrderServiceImpl();
		int result = iOrderService.saveOrder(order);
		afterMethod();
		return result;
	}

	public void beforeMethod(Order order) {

		int userId = order.getUserId();
		int dbRouter = userId % 2;
		System.out.println("静态代理分配到 【db" + dbRouter + "】处理数据");

		// todo 设置dataSource;
		DataSourceContextHolder.setDBType(String.valueOf(dbRouter));

		System.out.println("静态代理before code");
	}

	public void afterMethod() {
		System.out.println("静态代理after code");
	}

}
